<%@ page import="com.huan.pojo.User" %>
<%@ page import="java.text.SimpleDateFormat" %><%--
  Created by IntelliJ IDEA.
  User: aug_liu
  Date: 2021/2/23
  Time: 15:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增/修改用户</title>
    <link href="img/favicon.ico" rel="shortcut icon">
    <link href="./bootstrap-4.6.0-dist/css/bootstrap.css" rel="stylesheet" >
    <script src="js/jquery.min.js"></script>
    <script src="./bootstrap-4.6.0-dist/js/bootstrap.js"></script>

</head>
<body>
<div class="container-sm">
    <div class="row">
        <div class="col">
        </div>
        <div class="col-6">
            <div class="span12">
                <div class="row-fluid">
                    <div class="span6">
                        &nbsp;
                    </div>
                </div>
                <div class="row-fluid">
                    <div class="span6">
                        <h3>
                            用户表新增/修改
                        </h3>
                    </div>
                </div>
                <div class="row-fluid">
                    <div class="span6">
                        <%
                            String username = request.getParameter("username");
                            String usercode = request.getParameter("usercode");
                            String birthday = request.getParameter("birthday");

                            User user;
                            if (request.getAttribute("user")==null) {
                                user = new User();
                                if (request.getParameter("id") != null)
                                    user.setId(Integer.valueOf(request.getParameter("id")));

                                if (username != null && !username.isEmpty()) user.setUserName(username);
                                if (usercode != null && !usercode.isEmpty()) user.setUserCode(usercode);
                                if (birthday != null && !birthday.isEmpty()) user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
                            } else {
                                user = (User)request.getAttribute("user");
                            }

                            pageContext.setAttribute("user", user);
                        %>
                    </div>
                </div>
                <div class="row-fluid">
                    <div class="span6">
                        <p style="color: red">
                            ${errorMsg}
                        </p>
                    </div>
                </div>
                <form class="row g-1" action="userservlet" method="post">
                    <input type="hidden" name="id" value="${user.id}"/>
                    <input type="hidden" name="method" value="${user.id > 0 ? "updateUser" : "addUser" }"/>
                    <div class="col-md-12">
                        <label for="username" class="form-label">用户名：</label>
                        <input type="text" class="form-control" id="username" name="username" value="${user.userName}">
                    </div>
                    <div class="col-md-12">
                        <label for="usercode" class="form-label">用户Code：</label>
                        <input type="text" class="form-control" id="usercode" name="usercode" value="${user.userCode}">
                    </div>
                    <div class="col-md-12">
                        <label for="password" class="form-label">密码：</label>
                        <input type="password" class="form-control" id="password" name="password" value="${user.userPassword}">
                    </div>
                    <%
                        if (user.getId()<=0) {
                    %>
                    <div class="col-md-12">
                        <label for="confirmpassword" class="form-label">确认密码：</label>
                        <input type="password" class="form-control" id="confirmpassword">
                    </div>
                    <%
                        }
                    %>
                    <div class="col-md-12">
                        <label for="phone" class="form-label">电话：</label>
                        <input type="text" class="form-control" id="phone" name="phone" placeholder="##########" value="${user.phone}">
                    </div>
                    <div class="col-md-12">
                        <label for="address" class="form-label">地址：</label>
                        <input type="text" class="form-control" id="address" name="address"
                               placeholder="Apartment, studio, or floor" value="${user.address}">
                    </div>
                    <div class="col-md-12">
                        <label for="birthday" class="form-label">生日：</label>
                        <input type="date" class="form-control" id="birthday" name="birthday" value="${user.birthday}">
                    </div>
                    <div class="col-md-12">
                        <label for="role" class="form-label">角色：</label>
                        <select id="role" name="role" class="form-select">
                            <option value="1" ${user.userRole == 1 ? "selected" : ""}>系统管理员</option>
                            <option value="2" ${user.userRole == 2 ? "selected" : ""}>部门经理</option>
                            <option value="3" ${user.userRole == 3 ? "selected" : ""}>普通员工</option>
                        </select>
                    </div>

                    <div class="col-12">
                        <label class="form-label">性别:</label>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="gender" id="male" value="2" ${user.gender == 2 ? "checked" : ""}>
                            <label class="form-check-label" for="male">
                                男
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="gender" id="female" value="1" ${user.gender == 1 ? "checked" : ""}>
                            <label class="form-check-label" for="female">
                                女
                            </label>
                        </div>
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary">新增/修改</button>
                        &nbsp;
                        <a href="userservlet" class="btn btn-primary">返回</a>
                    </div>
                </form>
            </div>
        </div>
        <div class="col">
        </div>
    </div>
</div>

<jsp:include page="footer.jsp"/>
</body>
</html>
